package com.spvs.approval.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spvs.approval.domain.ApprovalSubmitEntity;
import com.spvs.approval.service.ApprovalSubmitService;
import com.spvs.common.core.web.domain.AjaxResult;
import com.spvs.common.security.utils.SecurityUtils;
import com.spvs.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;
import java.util.Map;


/**
 * 审批提交信息表
 */
@RestController
@RequestMapping("/approvalSubmit")
public class ApprovalSubmitController {
    @Autowired
    private ApprovalSubmitService basicApprovalSubmitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public AjaxResult list(@RequestParam Map<String, Object> params) {
        Page<ApprovalSubmitEntity> page = basicApprovalSubmitService.queryPage(params);

        return AjaxResult.success(page);
    }

    /**
     * 我的审核列表（待我审核）
     */
    @RequestMapping("/userApprovedList")
    public AjaxResult userApprovedList(@RequestParam Map<String, Object> params) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        params.put("userId", loginUser.getUserid());
        return AjaxResult.success(basicApprovalSubmitService.userApprovedList(params));
    }

    /**
     * 我的审核列表(由我发起)
     */
    @RequestMapping("/userSubmitList")
    public AjaxResult userSubmitList(@RequestParam Map<String, Object> params) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        params.put("userId", loginUser.getUserid());
        return AjaxResult.success(basicApprovalSubmitService.userSubmitList(params));
    }

    /**
     * 列表
     */
    @RequestMapping("/appPage")
    public AjaxResult appPage(@RequestParam Map<String, Object> params) {
        Page<ApprovalSubmitEntity> page = basicApprovalSubmitService.queryAppPage(params);
        return AjaxResult.success(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public AjaxResult info(@RequestParam Integer id) {
        ApprovalSubmitEntity basicApprovalSubmit = basicApprovalSubmitService.getById(id);

        return AjaxResult.success(basicApprovalSubmit);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public AjaxResult delete(@RequestBody Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            ApprovalSubmitEntity approvalSubmitOld = basicApprovalSubmitService.getById(ids[i]);
            if (approvalSubmitOld == null) {
                return AjaxResult.error(500, "存在异常记录，不可删除");
            }
            if (approvalSubmitOld.getApprovalStatus() > 1) {
                return AjaxResult.error(501, "存在审批已经开始记录，不可删除");
            }
        }
        basicApprovalSubmitService.removeByIds(Arrays.asList(ids));

        return AjaxResult.success();
    }


    /**
     * 待审批消息列表
     */
    @RequestMapping("/waitApproveList")
    public AjaxResult waitApproveList(@RequestParam Map<String, Object> params) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        params.put("userId", loginUser.getUserid());
        Page<ApprovalSubmitEntity> page = basicApprovalSubmitService.approvePage(params);

        return AjaxResult.success(page);

    }

    @RequestMapping("/selectApprovalSubmitVo")
    public AjaxResult selectApprovalSubmitVo(@RequestParam String businessId) {
        return basicApprovalSubmitService.selectApprovalSubmitVo(businessId);

    }
}
